﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

namespace HQWeb.SysManage.SecurityManage
{
    public partial class GetSecurity : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            VerdictIsLogin();
        }

        protected void btn_GetSecurity_Click(object sender, EventArgs e)
        {
            string Str_security = GenCode(4);
            Str_security += DateTime.Now.GetDateTimeFormats('f').ToString();
            Str_security = FormsAuthentication.HashPasswordForStoringInConfigFile(Str_security, "MD5").Substring(0, 8);
            Model.SysManage.T_PUB_SECURITY model = new Model.SysManage.T_PUB_SECURITY();
            BLL.SysManage.T_PUB_SECURITY bll = new BLL.SysManage.T_PUB_SECURITY();
            model.PRODUCER_ID = Convert.ToInt32(Session["USER_ID"]);
            model.PRODUCER_NAME = Session["USER_NAME"].ToString();
            model.SECURITY = Str_security;
            model.PRODUCE_TIME = DateTime.Now;
            if (bll.Add(model) != 0)
            {
                security.Text = Str_security;
            }
            else
            {
                Universal.JS.Alert("获取密码失败，请重新获取！");
            }
        }

        //产生随机字符串
        private string GenCode(int num)
        {
            string[] source ={"0","1","2","3","4","5","6","7","8","9",
                            "A","B","C","D","E","F","G","H","I","J","K","L","M","N",
                            "O","P","Q","R","S","T","U","V","W","X","Y","Z"};
            string code = "";
            Random rd = new Random();
            for (int i = 0; i < num; i++)
            {
                code += source[rd.Next(0, source.Length)];
            }
            return code;
        }

    }
}